﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Maticsoft.Common
{
    /// <summary>
    /// getchecknum 用来获取验证码中要显示的数字
    /// </summary>

    public sealed class getchecknum
    {
        private int len;
        private string checknumber;
        private const string RandCharString = "0123456789";

        /// <summary>
        /// getnum() 生成长度为length的验证码
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>

        public string getnum(int length)
        {
            this.len = length;
            Random random = new Random();
            do
            {
                ///使用DateTime.Now.Millisecond作为生成随机数的参数，增加随机性
                checknumber += RandCharString.Substring(random.Next(DateTime.Now.Millisecond) % RandCharString.Length, 1);
            } while (checknumber.Length < length);
            return checknumber;
        }
    }

    /// <summary>
    /// 根据验证码生成图片
    /// </summary>

    public sealed class RandImage
    {

        private int width;
        private int height;
        private int length;
        private string randString;

        /// <summary>
        /// 指定生成图片的宽和高以及生成图片的字符串字符个数
        /// </summary>

        public RandImage(int width, int height, int length, string randstring)
        {
            this.width = width;
            this.height = height;
            this.length = length;
            this.randString = randstring;
        }

        /// <summary>
        /// 以默认的大小和默认的字符个数产生图片
        /// </summary>
        /// <returns></returns>

        public Image GetImage()
        {
            Bitmap image = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);

            Random random = new Random();
            float emSize = (float)width / randString.Length;
            Font font = new Font("Arial", emSize, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            Pen pen = new Pen(Color.Silver);
            #region

            ///画图片的背景噪音线

            int x1, y1, x2, y2;

            for (int i = 0; i < 35; i++)
            {
                pen.Color = Color.FromArgb(random.Next(Int32.MaxValue));
                x1 = random.Next(image.Width);
                y1 = random.Next(image.Height);
                x2 = random.Next(image.Width);
                y2 = random.Next(image.Height);
                g.DrawLine(pen, x1, y1, x2, y2);
            }
            #endregion

            #region

            ///画图片的前景噪音点

            for (int i = 0; i < 150; i++)
            {
                x1 = random.Next(image.Width);
                y1 = random.Next(image.Height);
                image.SetPixel(x1, y1, Color.FromArgb(random.Next(Int32.MaxValue)));
            }
            #endregion

            g.DrawString(randString, font, Brushes.Gray, 5, 2);
            g.Dispose();
            return image;

        }
    }
}
